package com.lwj.liwanjia.view.activity.logistics;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.AddressBean;
import com.lwj.liwanjia.utils.KuaiDiDialog;
import com.lwj.liwanjia.utils.KuaiDiPayDialog;
import com.lwj.liwanjia.utils.WupinTypeDialog;
import com.lwj.liwanjia.view.activity.address.AddressActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 上门揽件
 */
public class CallKuaidiActivity extends BaseActivity {


    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_name)
    TextView tvName;//寄件人姓名
    @BindView(R.id.ji_hint)
    TextView jiHint;//寄件提示
    @BindView(R.id.jijian_address)
    RelativeLayout rlJi;//寄件地址
    @BindView(R.id.tv_jijian_address)
    TextView jiJianAddress;//地址内容
    @BindView(R.id.tv_shou_name)
    TextView tvShouName;//收件人姓名
    @BindView(R.id.shou_hint)
    TextView shouHint;//收件提示
    @BindView(R.id.shoujian_address)
    RelativeLayout rlShou;//收件地址
    @BindView(R.id.tv_shou_address)
    TextView shouJianAddress;//地址内容
    @BindView(R.id.rl_time)
    RelativeLayout rlTime;//上门时间
    @BindView(R.id.rl_yizhan)
    RelativeLayout rlYizhan;//附近驿站
    @BindView(R.id.tv_yizhan)
    TextView tvYizhan;//驿站
    @BindView(R.id.tv_time)
    TextView tvTime;//上门时间
    @BindView(R.id.tv_type)
    TextView tvType;//物品类型
    @BindView(R.id.cb_agree)
    CheckBox cbAgree;

    private String type;
    private List<String> list = new ArrayList<>();//上门时间
    private KuaiDiDialog kuaiDiDialog;//选择时间
    private WupinTypeDialog typeDialog;//选择物品类型
    private List<String> typeList = new ArrayList<>();//物品类型
    private KuaiDiPayDialog payDialog;//支付

    @Override
    protected void initView() {
        type = getIntent().getStringExtra("type");
        tvLeft.setVisibility(View.VISIBLE);
        if ("快递上门".equals(type)) {
            tvTitle.setText("上门揽件");
            rlYizhan.setVisibility(View.GONE);
            rlTime.setVisibility(View.VISIBLE);
        } else if ("驿站寄件".equals(type)) {
            tvTitle.setText("驿站寄件");
            rlTime.setVisibility(View.GONE);
            rlYizhan.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        list.add("09:00-11:00");
        list.add("11:00-13:00");
        list.add("11:00-15:00");
        list.add("15:00-17:00");
        list.add("17:00-19:00");
        typeList.add("日用品");
        typeList.add("数码相机");
        typeList.add("充电宝");
        typeList.add("空调");
        typeList.add("联想电脑");
        typeList.add("冰箱");
        typeList.add("洗衣机");
        typeList.add("牛肉");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_call_kuaidi;
    }


    @OnClick({R.id.tv_left, R.id.tv_submit, R.id.rl_ji, R.id.rl_shou, R.id.rl_time, R.id.rl_type, R.id.rl_yizhan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left://返回
                finish();
                break;
            case R.id.tv_submit://提交
                if (TextUtils.isEmpty(jiJianAddress.getText().toString()) || TextUtils.isEmpty(shouJianAddress.getText().toString())
                        || TextUtils.isEmpty(tvType.getText().toString())) {
                    Toast.makeText(this, "数据不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!cbAgree.isChecked()) {
                    Toast.makeText(this, "必须同意《快递服务协议》！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("快递上门".equals(type)) {
                    if (TextUtils.isEmpty(tvTime.getText().toString())) {
                        Toast.makeText(this, "数据不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } else if ("驿站寄件".equals(type)) {
                    if (TextUtils.isEmpty(tvYizhan.getText().toString())) {
                        Toast.makeText(this, "数据不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                payDialog = new KuaiDiPayDialog(this, "10.00");
                payDialog.show();
                payDialog.getPay().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int checkPosition = payDialog.getCheckPosition();
                        Toast.makeText(CallKuaidiActivity.this, "选择" + checkPosition, Toast.LENGTH_SHORT).show();
                        payDialog.dismiss();
                    }
                });
                break;
            case R.id.rl_ji://选择寄件地址
                Intent intent = new Intent(CallKuaidiActivity.this, AddressActivity.class);
                intent.putExtra("type", "快递");
                startActivityForResult(intent, 5);
                break;
            case R.id.rl_shou://选择收件地址
                Intent intent1 = new Intent(CallKuaidiActivity.this, AddressActivity.class);
                intent1.putExtra("type", "快递");
                startActivityForResult(intent1, 6);
                break;
            case R.id.rl_time://选择上门时间
                kuaiDiDialog = new KuaiDiDialog(this, list);
                kuaiDiDialog.show();
                kuaiDiDialog.getSure().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int checkPosition = kuaiDiDialog.getCheckPosition();
                        setTime(checkPosition);
                        kuaiDiDialog.dismiss();
                    }
                });
                break;
            case R.id.rl_type://选择物品类型
                typeDialog = new WupinTypeDialog(this, typeList);
                typeDialog.show();
                typeDialog.getSure().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int checkPosition = typeDialog.getCheckPosition();
                        if (checkPosition != -1) {
                            tvType.setText(typeList.get(checkPosition));
                            typeDialog.dismiss();
                        } else {
                            Toast.makeText(CallKuaidiActivity.this, "还未选择类型", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                });
                break;
            case R.id.rl_yizhan://选择驿站
                Intent intent2 = new Intent(this, YizhanActivity.class);
                startActivityForResult(intent2, 7);
                break;
        }
    }

    /**
     * 期望上门时间
     */
    private void setTime(int position) {
        switch (position) {
            case -2:
                tvTime.setText("30分钟内");
                //这里还得加个费用
                break;
            case -1:
                tvTime.setText("2小时内");
                break;
            default:
                tvTime.setText(list.get(position));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 5) {//寄件地址
                jiHint.setVisibility(View.GONE);
                rlJi.setVisibility(View.VISIBLE);
                AddressBean.DataBean address= (AddressBean.DataBean) data.getSerializableExtra("address");
                tvName.setText(address.getConsigneeName());
                jiJianAddress.setText(address.getConsigneeAddress());
            } else if (requestCode == 6) {//收货地址
                shouHint.setVisibility(View.GONE);
                rlShou.setVisibility(View.VISIBLE);
                AddressBean.DataBean address= (AddressBean.DataBean) data.getSerializableExtra("address");
                tvShouName.setText(address.getConsigneeName());
                shouJianAddress.setText(address.getConsigneeAddress());
            } else if (requestCode == 7) {//驿站
                tvYizhan.setText("真生活便利店");
            }
        }
    }
}
